﻿$.using("a2d.core.ajax");




a2d.core.ajax.ajaxwrapper = function (id, data, callback) {
    var defaultConfig = {
        id: null,
        data: null,
        callback: null
    };
    var realConfig = $.extend(defaultConfig, { id: id, data: data, callback: callback });
    var setupConfig = a2d.core.ajax.ajaxwrapper.setup.find(realConfig.id);

    var ajaxCall = function () {
        $.ajax({
            url: setupConfig.url,
            type: setupConfig.method,
            async: true,
            cache: false,
            data: realConfig.data,
            dataType: "json",
            success: realConfig.callback,
            error: a2d.core.exception.service.takeoverFunction(function () { throw new a2d.core.exception("ajax error"); })
        });
    }

    return {
        call: ajaxCall
    };
};

$.using("a2d.core.ajax.ajaxwrapper.setup");

a2d.core.ajax.ajaxwrapper.setup.rules = new Array();
a2d.core.ajax.ajaxwrapper.setup.find = function (id) {
    for (var i = 0; i < a2d.core.ajax.ajaxwrapper.setup.rules.length; i++)
        if (a2d.core.ajax.ajaxwrapper.setup.rules[i].id == id)
            return a2d.core.ajax.ajaxwrapper.setup.rules[i];

    return null;
};

a2d.core.ajax.ajaxwrapper.setup.add = function (ajaxCfg) {
    var defaultConfig = {
        id: null,
        url: null,
        method: "GET"
    };

    var realConfig = $.extend(defaultConfig, ajaxCfg);
    a2d.core.ajax.ajaxwrapper.setup.rules.push(realConfig);
};

